package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.cskaoyan.shopping.constants.GlobalConstants.*;

/**
 * @author xD
 * @since 2023/01/10 15:53
 */
@Service
public class IHomeServiceImpl implements IHomeService {

    @Resource
    private PanelMapper panelMapper;

    @Resource
    private PanelContentMapper panelContentMapper;

    @Resource
    private ContentConverter contentConverter;

    @Override
    public Set<PanelDto> homepage() {
        //首先查询tb_panel表
        Example example = new Example(Panel.class, true);
        example.orderBy(PANEL_SORT_ORDER);
        example.createCriteria()
                .andEqualTo("status", PANEL_STATUS)
                .andEqualTo("position", PANEL_POSITION);
        List<Panel> panels = panelMapper.selectByExample(example);
        Set<PanelDto> panelDtos = new HashSet<>();
        //给panel中的panelContentItems赋值
        for (Panel panel : panels) {
            //通过panelId 查询tb_panel_content 表
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            panel.setPanelContentItems(panelContentItems);
            //转换
            panelDtos.add(contentConverter.panel2Dto(panel));
        }
        return panelDtos;
    }
}
